#!/bin/python3
import os
import threading
def getGetPushAudioStreamCmd():
    res = os.popen('arecord -l').read()
    part1 = res.find('[USB PnP Sound Device]')
    part2 = res.find('[USB Audio]')
    part3 = res.find('\n', part1, part2)
    if part1 > 0 and part2 > 0 and part3  < 0:
        part4 = res.find('card')
        part5 = res.find('device')
        if(part4 > 0 and part5 >0):
            AudioDevice =  res[part4+5] + ','+ res[part5 +7]
            cmd = 'ffmpeg -f alsa -i plughw:' + AudioDevice + ' -acodec libmp3lame -ab 32k -ac 1 -f rtsp rtsp://127.0.0.1'
            return cmd;
def startEasyDarwin():
    if os.path.exists('/home/pi/go/src/github.com/EasyDarwin/EasyDarwin/easydarwin') and len(getGetPushAudioStreamCmd()) > 0:
        os.system('sudo /home/pi/go/src/github.com/EasyDarwin/EasyDarwin/easydarwin')
def startPushStream():
    if os.path.exists('/home/pi/go/src/github.com/EasyDarwin/EasyDarwin/easydarwin') and len(getGetPushAudioStreamCmd()) > 0:
        os.system(getGetPushAudioStreamCmd())

t1 = threading.Thread(target=startEasyDarwin)
t2 = threading.Thread(target=startPushStream)
t1.start()
t2.start()
t1.join()
t2.join()